<?php
/* ******************************
	Incoming Tasks aJax actions
   ******************************
*/
	include('../../includes/classes/core.php');
	$action = $_REQUEST['act'];
	$tab  	= $_REQUEST['tab'];
	$where  = "";
	if 	   ($tab=='0') $where  = "AND object.id = 131";
	elseif ($tab=='1') $where  = "AND object.id = 130";
	elseif ($tab=='2') $where  = "AND object.id = 154";
	elseif ($tab=='3') $where  = "AND object.id = 139";
	elseif ($tab=='4') $where  = "AND object.id = 132";
	elseif ($tab=='5') $where  = "AND object.id = 141";
	elseif ($tab=='6') $where  = "AND object.id = 140";
	switch ($action) {
	    case 'get_list':
    	    $count = $_REQUEST['count'];

    	    $rResult = mysql_query("SELECT  			reserve.id,
    	                                                production.name AS name,   														
														PAR.`name`, PAR1.`name`,    	                                                
														CONCAT(ROUND(SUM(reserve.quantity - reserve.sold), 2),' ', production_unit.name) as quantity,
														ROUND(reserve.price , 2) AS cost,
														ROUND((reserve.price * SUM( reserve.quantity - reserve.sold )) , 2) AS sumCost
									FROM  				reserve
									LEFT JOIN	object 		ON	reserve.object_id = object.id
									LEFT JOIN   production	ON	reserve.production_id = production.id
									LEFT JOIN   production_unit ON	production.unit = production_unit.id
                        	        LEFT JOIN   production_category ON production_category.id=production.category
                                    LEFT JOIN   production_category AS PAR ON production_category.parent_id=PAR.id
                                    LEFT JOIN   production_category AS PAR1 ON PAR.parent_id=PAR1.id
    	    						WHERE				reserve.quantity - reserve.sold > 0 AND ISNULL(reserve.barcode)  $where
									GROUP BY      		reserve.production_id
									");

			$output = array(
				"aaData"	=> array()
			);

			while ( $aRow = mysql_fetch_array( $rResult ) )
			{
				$row = array();
				for ( $i = 0 ; $i < $count ; $i++ )
				{
					/* General output */
					$row[] = $aRow[$i];
				}
				$output['aaData'][] = $row;
			}

			echo json_encode( $output );

	        break;
	    default:
	       	echo "null";
	}
?>